package com.estore.db.util;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import com.estore.db.beans.DBProperties;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBUtil {
	
	private DBUtil() {}
	
	private static ComboPooledDataSource cpds;
	private static DBProperties dbProps;
	
	public static void setDbProps(DBProperties props) {
		dbProps = props;
		
		cpds = new ComboPooledDataSource();
		try {
            cpds.setDriverClass(dbProps.getJdbcClass().trim());
            cpds.setJdbcUrl(dbProps.getJdbcURL().trim());
            cpds.setUser(dbProps.getUserName().trim());                                  
            cpds.setPassword(dbProps.getPassword().trim());
        } catch (PropertyVetoException e) {
            System.out.println("Invalid DB Property " + e);
        }  
	}

	public static Connection getConnection(){
	    Connection con = null;
	    try{
	        con = cpds.getConnection();
	    } catch (SQLException e) {
            System.out.println("Unable to obtain connection" + e);
        }
	    return con;
	}
}
